/**
 * class which represent the Mathematical Expression A^x
 * When A is himself Mathematical Expressions
 * and x is a real number which represents the exponent.
 * 
 * @author Nadav Cohen, Matan Monitz
 *
 */


public class Power implements Expression {
	private Expression baseExp;
	private double Exponent;
	/**
	 * constructor method for the power object
	 * 
	 * @param base an object implementing the expression interface representing the mathematical expression to be raised by the exponent. 
	 *  - must not be null
	 * @param exponent a double value representing the exponent
	 */
	public Power(Expression base, double exponent)
	{
		
		if(base==null)
			throw new RuntimeException("input error , base expression not initialized");
		this.Exponent = exponent;
		this.baseExp = base;
	}
	/**
	 * checks that the object is an instance of Power, that the exponent is the same and calls the equals method on the base expression field of the object
	 */
	public boolean equals(Object other)
	{
		if(other != null)
			{
			if(other instanceof Power)
				return (this.baseExp.equals(((Power)other).baseExp) && this.Exponent==((Power)other).Exponent);
			}
		return false;
	}
	public String toString()
	{
		return "(" + this.baseExp.toString() + "^" + this.Exponent + ")";
	}
	public double evaluate(Assignments assignments) {
		return Math.pow(this.baseExp.evaluate(assignments), this.Exponent);
	}
	
	public Expression derivative(Variable var) {
		//derivative of A^B = (B*(A)^B-1)*(A')
		return new Multiplication(new Multiplication(new Constant(this.Exponent),new Power(this.baseExp,this.Exponent-1)),this.baseExp.derivative(var));
	}

}
